import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-10-09
 * Time: 11:24
 */
public class demo2 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(1000);

        Thread producer = new Thread(() -> {
            int n = 0;
            while(true){
                try{
                    queue.put(n);
                    System.out.println("生产者元素" + n);
                    n++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "producer");

        Thread consumer = new Thread(() -> {
            while(true){
                try{
                    Integer n = queue.take();
                    System.out.println("消费者元素" + n);
                    Thread.sleep(2000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }, "consumer");

        producer.start();
        consumer.start();
    }
}
